package cn.citycraft.Multkills.config;

import java.io.File;
import java.io.IOException;

import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;

public class ItemConfig extends ConfigLoader {
	public static FileConfig instance;
	public static File file;
	private static String CONFIG_NAME = "item.yml";

	public ItemConfig(Plugin p, String ver) {
		super(p, CONFIG_NAME, ver);
		file = new File(p.getDataFolder(), CONFIG_NAME);
		instance = super.getInstance();
	}

	public static void load(Plugin p, String ver) {
		new ItemConfig(p, ver);
	}

	public static FileConfig getInstance() {
		return instance;
	}

	public static String getItemName(ItemStack i) {
		if (i.hasItemMeta() && i.getItemMeta().hasDisplayName()) {
			return i.getItemMeta().getDisplayName();
		}
		String name = i.getType().name();
		int dur = i.getDurability();
		String dura = i.getMaxStackSize() != 1 ? dur != 0 ? "_" + dur : "" : "";
		String iname = name + dura;
		String aname = instance.getString(iname);
		if (aname == null) {
			aname = iname;
			instance.set(iname, iname);
			try {
				instance.save(file);
			} catch (IOException e) {
				plugin.getLogger().info("配置文件" + file.getName() + "保存错误！");
				e.printStackTrace();
			}
		}
		return aname;
	}
	
	public static String getItemName(String iname) {
		String aname = instance.getString(iname);
		if (aname == null) {
			aname = iname;
			instance.set(iname, iname);
			try {
				instance.save(file);
			} catch (IOException e) {
				plugin.getLogger().info("配置文件" + file.getName() + "保存错误！");
				e.printStackTrace();
			}
		}
		return aname;
	}
	
}
